﻿using System.Web;

namespace VTCeBank.SSO.Utils
{
    public class SSOHelper
    {
        static string CACHE_KEY = "VTCeBank.SSO.Utils.CACHE_KEY";
        static string CACHE_KEY_FaceBook = "VTCeBank.SSO.Utils.CACHE_KEYFaceBook";
        public static string GetSSOKey()
        {
            return HttpRuntime.Cache.Get(CACHE_KEY) == null ? string.Empty : (string)HttpRuntime.Cache.Get(CACHE_KEY); ;
        }
        public static string GetSSOKeyFace()
        {
            return HttpRuntime.Cache.Get(CACHE_KEY_FaceBook) == null ? string.Empty : (string)HttpRuntime.Cache.Get(CACHE_KEY_FaceBook); ;
        }
        public static void RemoCache()
        {
            // add key with time out 24h.
            HttpRuntime.Cache.Remove(CACHE_KEY); ;
            HttpRuntime.Cache.Remove(CACHE_KEY_FaceBook);
        }
        public static void SetSSOKey(string ssoKey)
        {
            // add key with time out 24h.
          
            HttpRuntime.Cache.Insert(CACHE_KEY, ssoKey); ;
        }
        public static void SetSSOKeyFace(string ssoKey)
        {
            // add key with time out 24h.

            HttpRuntime.Cache.Insert(CACHE_KEY_FaceBook, ssoKey); ;
        }

        public static string URLLogin
        {
            get
            {
                return string.Format("{0}/accounts/account.login.aspx?sid={1}&ur={2}&m=1&continue={3}",
                    Constants.SSO_SERVER,            // SSO_SERVER_LOGIN address            
                    Constants.SERVICE_PROVIDER_SITE_ID,    // SERVICE_PROVIDER_SITE_ID hãy đăng ký với SSO server
                    HttpUtility.UrlEncode(HttpContext.Current.Request.Url.ToString()),     // ur
                    Constants.SSO_URL_CONTINUE      // URL Recived User Data
                   );
            }
        }


        public static string ReceiveUrl { get; set; }


        public static string URLCheckLogin
        {
            get
            {
                return string.Format("{0}/sso/login/?sid={1}&ur={2}&m=0&continue={3}",
                        Constants.SSO_SERVER,            // SSO_SERVER_LOGIN address            
                        Constants.SERVICE_PROVIDER_SITE_ID,    // SERVICE_PROVIDER_SITE_ID hãy đăng ký với SSO server
                        HttpUtility.UrlEncode(HttpContext.Current.Request.Url.ToString()),     // ur,
                        Constants.SSO_URL_CONTINUE      // URL Recived User Data
                      );
            }
        }

        public static string URLLogout
        {
            get
            {
                return string.Format("{0}/accounts/sso/logout/?ur={1}", Constants.SSO_SERVER, HttpUtility.UrlEncode(HttpContext.Current.Request.Url.ToString()));
            }
        }

        public static string GetURLLogout(string returnUrl)
        {

            return string.Format("{0}/accounts/sso/logout/?ur={1}", Constants.SSO_SERVER, HttpUtility.UrlEncode(returnUrl));

        }

        public static string GetURLLogin(string returnUrl)
        {
            return string.Format("{0}/accounts/sso/login/?sid={1}&ur={2}&m=1&continue={3}",
                Constants.SSO_SERVER,                   // SSO_SERVER_LOGIN address            
                Constants.SERVICE_PROVIDER_SITE_ID,    // SERVICE_PROVIDER_SITE_ID hãy đăng ký với SSO server
                HttpUtility.UrlEncode(returnUrl),      // ur
                Constants.SSO_URL_CONTINUE      // URL Recived User Data
               );
        }

        public static string GetLoginFacebook(string returnUrl)
        {
            //return string.Format("{0}/socialapi/default.aspx?sid={1}&ur={2}&m=1&continue={3}",
            //    Constants.SSO_SERVERFacebook,                   // SSO_SERVER_LOGIN address            
            //    Constants.SERVICE_PROVIDER_SITE_ID,    // SERVICE_PROVIDER_SITE_ID hãy đăng ký với SSO server
            //    HttpUtility.UrlEncode(returnUrl),      // ur
            //    Constants.SSO_URL_CONTINUEFacebook      // URL Recived User Data
            //   );

              return string.Format("{0}/accountapi/api/openid/login?ReturnUrl={1}",
                Constants.SSO_SERVERFacebook,                   // SSO_SERVER_LOGIN address
                HttpUtility.UrlEncode(returnUrl)     // ur                
               );            
        }


        public static string GetLoginFacebookApp(string returnUrl)
        {
            return string.Format("{0}/appfacebook/default.aspx?sid={1}&ur={2}&m=1&continue={3}",
                Constants.SSO_SERVER,                   // SSO_SERVER_LOGIN address            
                Constants.SERVICE_PROVIDER_SITE_ID,    // SERVICE_PROVIDER_SITE_ID hãy đăng ký với SSO server
                HttpUtility.UrlEncode(returnUrl),      // ur
                Constants.SSO_URL_CONTINUE      // URL Recived User Data
               );

        }
    }



}
